/**
 *  Copyright 2009-2010 Steven Ottenhoff (Dual-IT)
 * 
 *  This file is part of Clazzified.
 *
 *  Clazzified is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Clazzified is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Clazzified.  If not, see http://www.gnu.org/licenses/gpl.
 * 
 */
package nl.dualit.clazzified.store.clazz.db.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import nl.dualit.clazzified.store.clazz.AttributeFields;
import nl.dualit.clazzified.store.clazz.ClazzFields;
import nl.dualit.clazzified.store.clazz.ConstraintFields;
import nl.dualit.clazzified.store.clazz.IResultConverter;

import org.apache.log4j.Logger;

/**
 * Convert ResultSets to data maps of specifed objects.
 * 
 * @author steffo
 *
 */
class ResultConverterSql {
	private static final Logger log = Logger.getLogger( ResultConverterSql.class );
	
	static Map<ClazzFields,String> convertClazz( ResultSet result ){
		
		Map<ClazzFields,String> map = new HashMap<ClazzFields,String>();
		
		try {
			
			if( !result.next() )
				return null;		
			
			int i = 1;
			
			for( ClazzFields field : ClazzFields.values() )
				map.put( field, result.getString( i++ ) );
			
		} catch (SQLException e) {
			log.error( e.getMessage(), e );
			return null;
		}
				
		
		return map;
	}
	
	static List<Map<ClazzFields,String>> convertClazzes( ResultSet result ){
		List<Map<ClazzFields,String>> list = new ArrayList<Map<ClazzFields,String>>();
		
		try {
			while( !result.isAfterLast() ){
				
				list.add( convertClazz( result ) );
			
			}
			
			if( list.size() == 0 )
				return null;
			
			return list;
			
		} catch (SQLException e) {
			log.error( e.getMessage(), e );
			return null;
		}
		
	}
	
	static Map<AttributeFields,String> convertAttribute( ResultSet result ){
		Map<AttributeFields,String> map = new HashMap<AttributeFields,String>();
		
		try {
			
			if( !result.next() )
				return null;
			int i = 1;
			for( AttributeFields field : AttributeFields.values() )
				map.put( field, result.getString( i++ ) );
			
			return map;
			
		} catch (SQLException e) {
			log.error( e.getMessage(), e );
			return null;
		}		
	}
	
	static List<Map<AttributeFields,String>> convertAttributes( ResultSet result ){
		
		List<Map<AttributeFields,String>> list = new ArrayList<Map<AttributeFields,String>>();
		
		try {
			while( !result.isAfterLast() ){
				
				list.add( convertAttribute( result ) );
			}
			
			if( list.size() == 0 )
				return null;
			
			return list;
			
		} catch (SQLException e) {
			log.error( e.getMessage(), e );
			return null;
		}
	}
	
	static List<Map<ConstraintFields,String>> convertConstraints( ResultSet result ){
		List<Map<ConstraintFields,String>> list = new ArrayList<Map<ConstraintFields,String>>();
		try {
			while( result.next() ){
				Map<ConstraintFields,String> map = new HashMap<ConstraintFields,String>();
				
				int i = 1;
				for( ConstraintFields field : ConstraintFields.values() )
					map.put( field, result.getString( i++ ) );
				
				list.add( map );
			}
			
			if( list.size() == 0 )
				return null;
			
			return list;
			
		} catch (SQLException e) {
			log.error( e.getMessage(), e );
			return null;
		}
	}
}
